import Authorized from '@src/components/Auth';
import { IUser } from '@src/types/account';
import React from 'react';

// 路由项生成函数
const getRoutesItem = (
  path: string,
  component: React.ComponentType<{ userInfo: IUser } & any>,
  desc: string,
  needAuth: boolean,
  exact = true,
) => {
  // 内部鉴权组件
  const innerComponent: React.ComponentType<{ userInfo: IUser }> = needAuth
    ? ({ userInfo }: { userInfo: IUser }) => (
        <Authorized userInfo={userInfo}>{component}</Authorized>
      )
    : component;

  return {
    path,
    innerComponent,
    exact,
    desc,
  };
};

// 路由配置
const routesConfig = [
  getRoutesItem(
    '/chat/:chatId?', // 可选的参数
    React.lazy(() => import('../pages/chat/index')), // 动态引入,注意用了lazy之后要用suspend组件包起来,两者降级处理，https://react.caoweiju.com/src/advance/others.html
    '聊天',
    false,
  ),
  getRoutesItem(
    '/login',
    React.lazy(() => import('../pages/login/index')),
    '登录',
    false,
  ),
  getRoutesItem(
    '/register',
    React.lazy(() => import('../pages/register/index')),
    '注册',
    false,
  ),
  getRoutesItem(
    '/user',
    React.lazy(() => import('../pages/user/index')),
    '用户',
    true,
  ),
  getRoutesItem(
    '*',
    React.lazy(() => import('../pages/PageNotFound/indexTemp')),
    '404',
    false,
  ),
];

export default routesConfig;
